package ynu.lcy.elememicroserviceuserauthservice9091.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.User.RegisterDTO;
import ynu.lcy.elememicroservicecommon.annotation.AuthCheck;
import ynu.lcy.elememicroservicecommon.constant.UserConstant;
import ynu.lcy.elememicroservicecommon.exception.BusinessException;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ErrorCode;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;
import ynu.lcy.elememicroserviceuserauthservice9091.entity.User;
import ynu.lcy.elememicroserviceuserauthservice9091.service.UserService;


import ynu.lcy.vo.UserVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

@Api(tags = "用户接口接口")
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService service;

    @Resource
    private ModelMapper modelMapper;

    /**
     * 用户注册
     * @param user user
     * @return bool
     * */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@ApiParam("用户注册信息") RegisterDTO user){
        return  ResultUtils.success(service.register(user));
    }

    /**
     * 获取当前用户
     *
     * @param httpServletRequest 请求对象
     * @return 当前用户
     * */
    @ApiOperation("获取当前用户")
    @GetMapping("/getLoginUser")
    public BaseResponse<UserVo> getLoginUser(HttpServletRequest httpServletRequest){
        User loginUser = service.getLoginUser(httpServletRequest);
        if (Objects.isNull(loginUser)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo UserVo = modelMapper.map(loginUser, UserVo.class);
        return  ResultUtils.success(UserVo);
    }

    /**
     * 获取全部用户
     * */
    @ApiOperation("获取所有用户")
    @GetMapping("/admin/list")
//    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<UserVo>> list(){
        return ResultUtils.success(service.list().stream().map(e->modelMapper.map(e,UserVo.class)).toList());
    }
}
